/**
 * @author chongw
 * @since 2021-06-01 14:24:29
 */
layui.define(['form', 'layedit', 'laydate', 'zmnSelect', 'zmnApp', 'zmnBind'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let zmnBind = layui.zmnBind;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        sort: function (value) {
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        },
        integer: [
            /^[0-9]*([.][0-9]{0,2})?$/
            , '只能输入正整数或小数点后保留两位'
        ]
    });

    zmnSelect.selectlOrganizes({
        platSelectId: 'plat',
        orgThrSelectId: 'orgThrId'
    }, {
        platSelectId: {
            selectedValues: json.plat
        },
        orgThrSelectId: {
            selectedValues: json.orgThrId
        }
    }, {
        isRadio: true
    });
    staffSelect.init('receiverId', json.receiverName);
    /*zmnSelect.searchStaff('receiverId', {
        searchedValue: json.receiverName,
        selectedValues: json.receiverId,
        isRadio: true,
        remote: {
            limitPermit: json.receiverName,
            events: {
                extendParams: function () {
                    return {}
                }
            }},
        events: {
            changed: function (values) {

            }
        }
    });*/

    let customSearch = (selectId, options) => {
        let newOptions = zmnSelect.mergeOptions(selectId, options, {
            propMap: {  //选项参数映射配置
                name: "groupName",   //显示值
                value: "robotId"  //选中值
            },
            isRadio: true,
            remote: {
                enable: true,
                url: '/ding/group/robot/listAll',
                events: {
                    params: function (searchValue, limitPermit) {
                        return {
                            name: searchValue,
                        }
                    }
                }
            }
        })
        return zmnSelect.selectBox(selectId, newOptions);
    }

    let searchBoxDemo = customSearch('robotId', {
        name: "robotId",
        //searchedValue: json.vo.demo4SearchValue,
        selectedValues: json.robotId, //已选中值（用于回显)
        /*events: {
            changed: function (values) {
                console.log("==========自定义单个搜索下拉框=========");
                console.log("已选中的值:" + values);
            }
        }*/
    })
    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let index;
        let data = obj.field;
        data.platName = $("#plat .label-content").attr("title");
        data.orgThrName = $("#orgThrId .label-content").attr("title");
        data.groupName = $("#robotId .label-content").attr("title");
        data.receiverName = getSelectItem('receiverId');
        data.receiverId = getSelectItemIdStr(data.receiverName);
        if (data.receiverId == "") {
            layer.msg("钉钉接收人不能为空，请选择钉钉接收人！", {icon: 2});
            return false;
        }
        if (data.robotId == "") {
            layer.msg("钉钉群不能为空，请选择钉钉群！", {icon: 2});
            return false;
        }
        $.ajax({
            async: false,
            type: "POST",
            url: "/brs/engineer/rule/unpaid/addEdit.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (res) {
                if (res.status === 200) {
                    layer.msg((data.thresholdId ? "修改" : "新增") + "配置规则成功！", {"icon": 1, "time": 1000}, function () {
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.message, {"icon": 2});
                }
                layer.close(index);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
        return false;
    });

    //对外暴露的接口
    exports('brs_engineer_rule_unpaid_detail', {});
});

// 取消
function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}